package com.kaiwukj.android.ufamily.mvp.ui.page.home.home.presenter

import com.kaiwukj.android.mcas.di.scope.FragmentScope
import com.kaiwukj.android.mcas.mvp.BasePresenter
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils
import com.kaiwukj.android.ufamily.app.MyApplication
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp
import com.kaiwukj.android.ufamily.mvp.http.entity.params.CommentParams
import com.kaiwukj.android.ufamily.mvp.http.entity.params.DynamicListParams
import com.kaiwukj.android.ufamily.mvp.http.entity.result.*
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils
import com.kaiwukj.android.ufamily.mvp.ui.page.home.home.HomeData01Zip
import com.kaiwukj.android.ufamily.mvp.ui.page.home.home.contract.HomeV20Contract
import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Function3
import io.reactivex.functions.Function5
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import javax.inject.Inject


const val DYNAMIC_LIKE: Int = 1//点赞
const val DYNAMIC_LIKE_CANCEL: Int = 2//取消点赞
const val ATTENTION: Int = 3//关注
const val BLOCK_USER: Int = 10
const val BLOCK_DYNAMIC: Int = 11
const val DYNAMIC_DELETE = 12;

@FragmentScope
class HomeV20Presenter
@Inject
constructor(model: HomeV20Contract.Model, rootView: HomeV20Contract.View)
    : BasePresenter<HomeV20Contract.Model, HomeV20Contract.View>() {

    @Inject
    lateinit var mErrorHandler: RxErrorHandler


    private fun getCommunityInfo(communityId: Int): Observable<CommunityInfoResult> {
        return mModel.getCommunityInfo(communityId).compose(RxUtils.schedulerHelper())
    }

    private fun fetchNotice(): Observable<ListResp<CommunityNoticeResult>> {
        return mModel.fetchNotice()
                .compose(RxUtils.schedulerHelper())
    }

    private fun fetchDynamic(params: DynamicListParams): Observable<ListResp<DynamicResult>> {
        return mModel.fetchDynamic(params)
                .compose(RxUtils.schedulerHelper())
    }

    private fun fetchAttentionDynamic(startId: Int, pageSize: Int, type: Int): Observable<ListResp<DynamicResult>> {
        return mModel.fetchAttentionDynamic(startId, pageSize, type)
                .compose(RxUtils.schedulerHelper())
    }

    private fun fetchHotActive(type: Int): Observable<List<ActiveResult>> {
        return mModel.fetchHotActive(type)
                .compose(RxUtils.schedulerHelper())
    }

    private fun fetchFriendReco(type: Int): Observable<MutableList<FriendRecoResult>> {
        return mModel.fetchRecoAttention(type)
    }

    fun refreshFriendReco(index: Int) {

        val type = if (index == 1) {
            1
        } else {
            2
        }

        mModel.fetchRecoAttention(type)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<MutableList<FriendRecoResult>>() {
                    override fun onSuccess(result: MutableList<FriendRecoResult>) {
                        mRootView.onFetchFriend(result)
                    }
                })
    }

    private fun initHomeData(dynamicParams: DynamicListParams) {
        val communityId = MyApplication.getInstance().communityId
        val scope = 1
        dynamicParams.type = scope
        Observable.zip(mModel.getCommunityInfo(communityId),
                //  mModel.fetchNotice(),
                mModel.fetchRecoAttention(scope),
                mModel.fetchDynamic(dynamicParams),
                mModel.fetchHotActive(scope),
                mModel.fetchCommunityNews(communityId),
                Function5<CommunityInfoResult, MutableList<FriendRecoResult>,
                        ListResp<DynamicResult>, List<ActiveResult>, ListResp<CommunityNewResult>, HomeData01Zip> { i1, i3, i4, i5, i6 ->
                    HomeData01Zip().apply {
                        communityInfoResult = i1
                        // noticeResults = i2.list
                        friendRecoResults = i3
                        dynamicResults = i4.list
                        activeResults = i5
                        communityNewsResult = i6.list
                    }
                })
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<HomeData01Zip>(mRootView) {
                    override fun onSuccess(result: HomeData01Zip) {
                        mRootView.onFetchData(result)
                    }
                })
    }

    private fun initHomeAttData(dynamicParams: DynamicListParams) {
        val scope = 2
        dynamicParams.type = scope
        Observable.zip(mModel.fetchRecoAttention(scope),
                mModel.fetchAttentionDynamic(dynamicParams.startId, dynamicParams.pageSize, 2),
                BiFunction<MutableList<FriendRecoResult>, ListResp<DynamicResult>, HomeData01Zip> { i1, i2 ->
                    HomeData01Zip().apply {
                        friendRecoResults = i1
                        dynamicResults = i2.list
                    }
                })
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<HomeData01Zip>(mRootView) {
                    override fun onSuccess(result: HomeData01Zip) {
                        mRootView.onFetchData(result)
                    }
                })
    }

    private fun initHomeCityData(dynamicParams: DynamicListParams) {
        val scope = 2;
        dynamicParams.type = scope
        Observable.zip(mModel.fetchRecoAttention(scope),
                mModel.fetchHotActive(scope), mModel.fetchDynamic(dynamicParams),
                Function3<MutableList<FriendRecoResult>, List<ActiveResult>, ListResp<DynamicResult>,
                        HomeData01Zip> { i1, i2, i3 ->
                    HomeData01Zip().apply {
                        friendRecoResults = i1
                        activeResults = i2
                        dynamicResults = i3.list
                    }
                })
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<HomeData01Zip>(mRootView) {
                    override fun onSuccess(result: HomeData01Zip) {
                        mRootView.onFetchData(result)
                    }
                })
    }

    fun initHomeData(dynamicParams: DynamicListParams, index: Int) {
        when (index) {
            0 -> {
                initHomeAttData(dynamicParams)
            }
            1 -> {
                initHomeData(dynamicParams)
            }
            2 -> {
                initHomeCityData(dynamicParams)
            }
        }
    }


    private fun getDataZip(dynamicResults: List<DynamicResult>, friendRecoResults: MutableList<FriendRecoResult>): HomeData01Zip {
        val dataZip = HomeData01Zip()
        dataZip.dynamicResults = dynamicResults;
        dataZip.friendRecoResults = friendRecoResults;
        return dataZip
    }

    fun initHome02Data(dynamicParams: DynamicListParams, type: Int) {
        Observable.zip(fetchAttentionDynamic(dynamicParams.startId, dynamicParams.pageSize, type), fetchFriendReco(type),
                BiFunction<ListResp<DynamicResult>, MutableList<FriendRecoResult>, HomeData01Zip> { i1, i2 ->
                    getDataZip(i1.list, i2)
                }).compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<HomeData01Zip>() {
                    override fun onSuccess(result: HomeData01Zip) {
                        mRootView.onFetchData(result)
                    }
                })
    }

    fun fetchHomeMoreDynamic(params: DynamicListParams, index: Int) {
        when (index) {
            0 -> {
                params.type = 2
                fetchHome02Dynamic(params)
            }
            1 -> {
                params.type = 1
                fetchHomeDynamic(params)
            }
            2 -> {
                params.type = 2
                fetchHomeDynamic(params)
            }
        }
    }

    fun fetchHomeDynamic(params: DynamicListParams) {
        fetchDynamic(params)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<ListResp<DynamicResult>>(mErrorHandler, mRootView) {
                    override fun onSuccess(result: ListResp<DynamicResult>) {
                        mRootView.onFetchDynamic(result.list)
                    }
                })

    }

    private fun fetchHome02Dynamic(params: DynamicListParams) {
        fetchAttentionDynamic(params.startId, params.pageSize, params.type)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<ListResp<DynamicResult>>(mErrorHandler, mRootView) {
                    override fun onSuccess(result: ListResp<DynamicResult>) {
                        mRootView.onFetchDynamic(result.list)
                    }
                })

    }

    fun postLike(dynamicId: Int) {
        mModel.postLike(dynamicId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<Int>(mErrorHandler, mRootView, DYNAMIC_LIKE) {
                    override fun onSuccess(result: Int) {
                    }
                })
    }

    fun postLikeCancel(dynamicId: Int) {
        mModel.postNoLike(dynamicId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<Int>(mErrorHandler, mRootView, DYNAMIC_LIKE_CANCEL) {
                    override fun onSuccess(result: Int) {

                    }
                })
    }

    fun postAttention(userId: Int) {
        mModel.attention(userId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<Int>(mErrorHandler, mRootView, ATTENTION) {
                    override fun onSuccess(result: Int) {
                    }
                })
    }

    fun postComment(params: CommentParams) {
        mModel.comment(params)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<Int>(mErrorHandler, mRootView) {
                    override fun onSuccess(result: Int) {
                        mRootView.showMessage("评论成功")
                        mRootView.onCommentResult(result, params)
                    }
                })
    }

    fun postBlockUserOrDynamic(id: Int, isUser: Boolean) {
        mModel.shield(id, true, isUser)
                //  .doOnSubscribe { mRootView.showLoading("正在请求...") }
                //  .doFinally { mRootView.hideLoading() }
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<Int>(mErrorHandler, mRootView, if (isUser) BLOCK_USER else BLOCK_DYNAMIC) {
                    override fun onSuccess(result: Int) {
                        mRootView.showMessage("操作成功")
                    }
                })
    }

    fun postDelDynamic(dynamicId: Int) {
        mModel.delDynamic(dynamicId)
                .doOnSubscribe { mRootView.showLoading("正在请求...") }
                .doFinally { mRootView.hideLoading() }
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(object : ApiCallBack<Int>(mErrorHandler, mRootView, DYNAMIC_DELETE) {
                    override fun onSuccess(result: Int) {
                        mRootView.showMessage("操作成功")
                    }
                })
    }

}